package javaapplication1;

import java.io.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class IO {

    private static File file;

    public static void writeToFile(String fileName, String text, Boolean isAppend) {
        file = new File(fileName);

        try {
            if (file.exists()) {
                if (isAppend) {
                    StringBuilder sb = new StringBuilder("");
                    String oldFile = readFromFile(fileName);
                    sb.append(oldFile);
                    sb.append(text);
                    text = sb.toString();
                } else {
                    file.createNewFile();
                }
            }

            PrintWriter out = new PrintWriter(file.getAbsoluteFile());

            try {
                out.print(text);
            } finally {
                out.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static void exists(String fileName) throws FileNotFoundException {
        File file = new File(fileName);
        if (!file.exists()) {
            throw new FileNotFoundException(file.getName());
        }
    }

    public static String readFromFile(String fileName) throws FileNotFoundException {
        file = new File(fileName);
        StringBuilder sb = new StringBuilder();

        exists(fileName);

        try {
            BufferedReader in = new BufferedReader(new FileReader(file.getAbsoluteFile()));
            try {
                String s;
                while ((s = in.readLine()) != null) {
                    sb.append(s);
                    sb.append("\n");
                }
            } finally {
                in.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return sb.toString();
    }

    public static void createFile(String fileName) {
        writeToFile(fileName, "", false);
    }

    public static void deleteFile(String fileName) throws FileNotFoundException {
        file = new File(fileName);
        if (file.exists()) {
            file.delete();
        }
    }

    public static void compressFile(String fileName) {
        byte[] buffer = new byte[1024];
        
        file = new File(fileName);

        try {
            FileOutputStream fos = new FileOutputStream(fileName + ".zip");
            ZipOutputStream zos = new ZipOutputStream(fos);
            ZipEntry ze = new ZipEntry(file.getName());
            zos.putNextEntry(ze);
            FileInputStream in = new FileInputStream(fileName);

            int len;
            while ((len = in.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }

            in.close();
            zos.closeEntry();

            zos.close();

            System.out.println("Done");

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
